﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;

namespace KZ.CustomControl
{
    public delegate void AfterRowSelectEventHandler(object sender, object value);

    public class MultiColumnDropDown : Form
    {
        private string _valueMember = String.Empty;
        private string _displayMember = String.Empty;
        private const string _VALUEPREFIX = "V_";
        private const string _LISTNAME = "List`1";

        private DataGridView dtgData;
        private BindingSource bdsSource;
        private System.ComponentModel.IContainer components;

        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle7 = new System.Windows.Forms.DataGridViewCellStyle();
            System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle8 = new System.Windows.Forms.DataGridViewCellStyle();
            this.dtgData = new System.Windows.Forms.DataGridView();
            this.bdsSource = new System.Windows.Forms.BindingSource(this.components);
            ((System.ComponentModel.ISupportInitialize)(this.dtgData)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.bdsSource)).BeginInit();
            this.SuspendLayout();

            //
            // dtgData
            //
            this.dtgData.AllowUserToAddRows = false;
            this.dtgData.AllowUserToDeleteRows = false;
            this.dtgData.AllowUserToResizeRows = false;
            dataGridViewCellStyle7.BackColor = System.Drawing.Color.Azure;
            this.dtgData.AlternatingRowsDefaultCellStyle = dataGridViewCellStyle7;
            this.dtgData.AutoGenerateColumns = false;
            this.dtgData.BackgroundColor = System.Drawing.SystemColors.Window;
            dataGridViewCellStyle8.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
            dataGridViewCellStyle8.BackColor = System.Drawing.SystemColors.Control;
            dataGridViewCellStyle8.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            dataGridViewCellStyle8.ForeColor = System.Drawing.SystemColors.WindowText;
            dataGridViewCellStyle8.SelectionBackColor = System.Drawing.SystemColors.Highlight;
            dataGridViewCellStyle8.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
            dataGridViewCellStyle8.WrapMode = System.Windows.Forms.DataGridViewTriState.True;
            this.dtgData.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle8;
            this.dtgData.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dtgData.DataSource = this.bdsSource;
            this.dtgData.Dock = System.Windows.Forms.DockStyle.Fill;
            this.dtgData.Location = new System.Drawing.Point(0, 0);
            this.dtgData.MultiSelect = false;
            this.dtgData.Name = "dtgData";
            this.dtgData.ReadOnly = true;
            this.dtgData.RowHeadersVisible = false;
            this.dtgData.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
            this.dtgData.Size = new System.Drawing.Size(292, 273);
            this.dtgData.TabIndex = 0;
            this.dtgData.CellMouseClick += new System.Windows.Forms.DataGridViewCellMouseEventHandler(this.dtgData_CellMouseClick);
            this.dtgData.CellMouseEnter += new System.Windows.Forms.DataGridViewCellEventHandler(this.dtgData_CellMouseEnter);

            //
            // MultiColumnDropDown
            //
            this.ClientSize = new System.Drawing.Size(292, 273);
            this.ControlBox = false;
            this.Controls.Add(this.dtgData);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            this.Name = "MultiColumnDropDown";
            this.ShowInTaskbar = false;
            this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
            this.TopMost = true;
            this.Deactivate += new System.EventHandler(this.MultiColumnDropDown_Deactivate);
            this.VisibleChanged += new System.EventHandler(this.MultiColumnDropDown_VisibleChanged);
            ((System.ComponentModel.ISupportInitialize)(this.dtgData)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.bdsSource)).EndInit();
            this.ResumeLayout(false);
        }

        private void MultiColumnDropDown_VisibleChanged(object sender, EventArgs e)
        {
            if (this.Visible)
                this.Activate();
        }

        private void MultiColumnDropDown_Deactivate(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void dtgData_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex != -1) { dtgData.Rows[e.RowIndex].Selected = true; }
        }

        private void dtgData_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.ColumnIndex != -1 && e.RowIndex != -1)
            {
                object value = null;
                if (dtgData.Columns.Contains(_valueMember)) { value = dtgData[_valueMember, e.RowIndex].Value; }

                AfterRowSelectEvent(dtgData, value);
                this.Hide();
            }
        }

        private void SetHeight(int itemCount)
        {
            if (itemCount < 20) { this.Height = dtgData.ColumnHeadersHeight + itemCount * 22; }
            else { this.Height = dtgData.ColumnHeadersHeight + 400; }
        }

        public MultiColumnDropDown()
        {
            InitializeComponent();
            dtgData.AutoGenerateColumns = false;
        }

        public object Source
        {
            set
            {
                bdsSource.DataSource = null;
                bdsSource.DataSource = value;
                if (value is DataTable)
                {
                    DataTable dt = value as DataTable;
                    SetHeight(dt.Rows.Count);
                }
                else
                {
                    Type type = value.GetType();
                    if (type.Name == _LISTNAME)
                    {
                        IList collection = (IList)value;
                        SetHeight(collection.Count);
                    }
                }
            }
        }

        public string ValueMember
        {
            set
            {
                if (!String.IsNullOrEmpty(value))
                {
                    _valueMember = _VALUEPREFIX + value;
                    dtgData.Columns.Add(_VALUEPREFIX + value, value);
                    dtgData.Columns[_VALUEPREFIX + value].DataPropertyName = value;
                    dtgData.Columns[_VALUEPREFIX + value].Visible = false;
                }
            }
        }

        public string DisplayMember
        {
            set { _displayMember = value; }
        }

        public Dictionary<string, string> DisplayColumns
        {
            set
            {
                foreach (KeyValuePair<string, string> item in value)
                {
                    dtgData.Columns.Add(item.Key, item.Value);
                    dtgData.Columns[item.Key].DataPropertyName = item.Key;
                }

                dtgData.Columns[dtgData.ColumnCount - 1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            }
        }

        public event AfterRowSelectEventHandler AfterRowSelectEvent;
    }
}